package BinSearchTree;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 86183
 * Date: 2024-05-25
 * Time: 17:32
 */



/**
 * 二叉搜索树---二叉排序树
 */
public class BinarySearchTree {
    static class TreeNode {
        public int val;
        public TreeNode left;
        public TreeNode right;

        public TreeNode(int val) {
            this.val = val;
        }
    }
        public TreeNode root;

        /**
         * 查找
         * @param val
         */
        public boolean search(int val){
            if(root == null){
                return false;
            }
            TreeNode cur = root;
            while(cur != null){
                if(cur.val < val){
                    cur = cur.right;
                }else if(cur.val > val){
                    cur = cur.left;
                }else{
                    return true;
                }
            }
            return false;

        }
        /**
         * 插入
         */
        public void insert (int val){
            TreeNode node = new TreeNode(val);
            if(root == null){
                root = node;
                return;
            }
            TreeNode cur = root;
            TreeNode parent = cur;
            while(cur != null){
                if(cur.val < val){
                    parent =cur;
                    cur = cur.right;
                }else if(cur.val > val){
                    parent = cur;
                    cur = cur.left;
                }else{
                    return;
                }
            }
            if(parent.val < val){
                parent.right = node;
            }else{
                parent.left = node;
            }

        }

        /**
         * 删除
         * @param val
         */
        public void remove(int val){
            TreeNode cur =root;
            TreeNode parent = null;
            while(cur != null){
                if(cur.val < val){
                    parent = cur;
                    cur = cur.right;
                }else if(cur.val > val){
                    parent =  cur;
                    cur =cur.left;
                }else {
                    break;
                }
            }
            if(cur.left == null){
                if(cur == root){
                    root = root.right;

                }else if(cur == parent.left){
                    parent.left = cur.right;
                }else{
                    parent.right = cur.right;
                }
            }else if(cur.right == null){
                if(cur == root){
                    root = cur.left;
                }else if(cur == parent.left){
                    parent.left  = cur.left;

                }else {
                    parent.right =cur.left;
                }
            }else{
                TreeNode t = cur.right;
                TreeNode tp = cur;
                while(t.left !=null) {
                    tp = t;
                    t = t.left;
                }
               cur.val = t.val;
                if(t == tp.left){
                    tp.left = t.right;
                }else{
                    tp.right = t.right;
                }


            }

        }
//        private void swap(TreeNode i, TreeNode j){
//          int tmp = i.val;
//          i.val = j.val;
//          j.val = tmp;
//
//        }


}
